package com.jz.cost.conroller;

import com.jz.cost.model.CostAddVo;
import com.jz.cost.model.CostSelectVo;
import com.jz.cost.model.CostUpdateStatusVo;
import com.jz.cost.model.CostUpdateVo;
import com.jz.cost.service.CostService;
import com.jz.partclass.model.PartUpdateStatusVo;
import com.jz.util.ResultBuildVo;
import com.jz.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * Created by WISE on 2021/12/26.
 */

@CrossOrigin//开启跨域
@RestController
public class CostController {


    //1.首页分页
    @Autowired
    private CostService costService;

    @PostMapping("materialCostPrice/queryPage")
    public ResultVo firstPage(@RequestBody CostSelectVo costSelectVo) {
        ResultVo resultVo = costService.firstPage(costSelectVo);

        return resultVo;
    }





    //计划大类下拉框
    @GetMapping("tax")
    public ResultVo tax() {

        return costService.queryTax();
    }

//
    //2.新增
    @PostMapping("materialCostPrice/add")
    @Transactional
    public ResultVo add(@RequestBody @Valid CostAddVo costAddVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = costService.addCost(costAddVO);
        return resultVo;
    }

//
    //3.修改
@PostMapping("materialCostPrice/update")
public ResultVo update(@RequestBody @Valid CostUpdateVo costUpdateVo, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
        return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
    }
    return costService.updateCost(costUpdateVo);
}
@PostMapping("materialCostPrice/updateStatus")
public ResultVo updateStatus(@RequestBody @Valid CostUpdateStatusVo costUpdateStatusVo, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
        return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
    }
    return costService.updateStatus(costUpdateStatusVo);
}




}



